<?php
namespace App\Libs\Helper;

class SmsHelper extends \App\Libs\Base\Singleton
{
    private $auth = '';
    private $tm = '';
    private $config;
    protected function __construct() {
        $this->tm = date('YmdHis000');
        $this->config = \App\Libs\Base\Config::instance()->sms;
        $this->auth = $this->genAuth();
        $this->sig = $this->genSig();

        
    }

    /**
     *
     * @return TRUE/FALSE 成功/失败
     */
    public function send($mobile, $content , $tplId ) {
        $api = 'maap/sms/code';

        $headers = array(
            'Host:www.ucpaas.com',
            'Accept:application/json',
            'Content-Type:application/json;charset=utf-8',
            'Authorization:' . $this->auth,
        );

        $params = array(
            "sid"         => $this->config->sid, 
            "appId"       => $this->config->app_id,
            "time"        => $this->tm,
            "param"       => $content,
            "templateId"  => $tplId,
            "to"          => $mobile,
            'sign' => $this->sig,
            );

        $response = \App\Libs\Remote\Remote::instance()->request('sms', $api, $params, $headers);
        
        \App\Libs\Log\Logger::error('send_sms', json_encode($response));
        return FALSE;
    }


    public function genSig() {
        return md5($this->config->sid . $this->tm . $this->config->token);
    }

    public function genAuth() {
        return base64_encode($this->config->app_id . ':' . $this->tm);
    }
}

